package br.com.eresinformatica.prototipo.dados;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class Download extends Activity {

	private MenuItem item;
	private String url = "http://10.0.2.2:8080/baixando_imagem/Imagem/get";
	private String name = "Camaro.jpg";
	ImageView imgView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_download);

		imgView = (ImageView) findViewById(R.id.imgView1);
		String[] params = new String[] { url, name };

		SendHttpRequestTask task = new SendHttpRequestTask();
		task.execute(params);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.download, menu);
		item = menu.getItem(0);
		return true;
	}

	private class SendHttpRequestTask extends AsyncTask<String, Void, byte[]> {
		@Override
		protected byte[] doInBackground(String... params) {
			String url = params[0];
			String name = params[1];

			HttpClient client = new HttpClient(url);
			byte[] data = client.downloadImage(name);

			return data;
		}

		@Override
		protected void onPostExecute(byte[] result) {
			Bitmap img = BitmapFactory
					.decodeByteArray(result, 0, result.length);
			imgView.setImageBitmap(img);
			item.setActionView(null);

		}

	}

}
